/*
 * bus_net.h
 *
 *  Created on: 2013-10-22
 *      Author: dj
 */

#ifndef BUS_NET_H_
#define BUS_NET_H_

#include "config.h"
#include "queue.h"

#define		SERVER_ID_MASK(id)							((id) & 0x01000000)
#define		DEVICE_ID_MASK(id)							((id) & 0x02000000)

enum {
	SERVER_TALKBACK_COMMAND = SERVER_ID_MASK(0x01), /*the talkback/monitor command from server*/
	SERVER_ROLL_COMMAND, /*the call the roll command from server*/
	SERVER_SNAP_COMMAND, /*the snap command from server*/
	SERVER_REQUEST_VERSION_COMMAND, /*the command of server requests device version*/
	SERVER_ALARM_CLEAR_COMMAND, /*the command of server clears alarm*/
	SERVER_OIL_CONTROL_COMMAND, /*the command of server control oil*/
	SERVER_ADD_BUSINESS_CONTROL_COMMAND, /*the command of server control to add business*/
	SERVER_EXIT_BUSINESS_CONTROL_COMMAND, /*the command of server control to exit business*/
	SERVER_SEND_SCHEDULING_COMMAND, /*the command of server send character schedul information*/
	SERVER_CHECK_IN_RESPONSE, /*the command of server response driver check in*/
	SERVER_SEND_TOTAL_MILEAGE_COMMAND, /*the command of server send total mileage to device*/
	SERVER_REQUEST_LINE_NUMBER_COMMAND, /*the command of server request line number*/
	SERVER_UPGRADE_FILE_COMMAND, /*the command of server upgrade device*/
	SERVER_REQUEST_LOG_FILE_COMMAND, /*the command of server request log file in device*/
	SERVER_UPGRADE_VERIFY_RESPONSE, /*the command of server verify upgrade file*/
	SERVER_SET_LINE_NUMBER_COMMAND, /*the command of server set line number*/
	SERVER_SEND_SHOW_CHARACTER_COMMAND, /*the command of server send character to show in LED immediately, that do not store*/
	SERVER_UPGRADE_SERVER_COMMAND, /*the command of server upgrade server*/
	SERVER_REQUEST_RECORDER_STATUS_COMMAND, /*the command of server request the status of recorder*/
	SERVER_TRACE_COMMAND, /*the command of server trace device*/
	SERVER_SET_DIRECTION_COMMAND, /*the command of server set the direction of device running*/
	SERVER_SERIAL_PORT_TRANSMITION, /*the command of server send transmittion data by serial port*/
	SERVER_SEND_INFORM_COMMAND, /*the command of server send inform content to device*/
	SERVER_SEND_COIN_MACHINE_COMMAND, /*the command of server send instruction to coin machine*/
	SERVER_PROCESS_PARAMETER_COMMAND, /*the command of server sets/gets device paramter*/

	SERVER_CFG_PARAMETER_CARTYPE = SERVER_ID_MASK(0x81),
	SERVER_CFG_PARAMETER_JPEGFMT,
	SERVER_CFG_PARAMETER_APN,
	SERVER_CFG_PARAMETER_CENT,
	SERVER_CFG_PARAMETER_USERNAME,
	SERVER_CFG_PARAMETER_PASSWORD,
	SERVER_CFG_PARAMETER_IPAD,
	SERVER_CFG_PARAMETER_IPSEC,
	SERVER_CFG_PARAMETER_PORT,
	SERVER_CFG_PARAMETER_PTSEC,
	SERVER_CFG_PARAMETER_RESDTM,
	SERVER_CFG_PARAMETER_RESDINT,
	SERVER_CFG_PARAMETER_CKPLINE,
	SERVER_CFG_PARAMETER_GPSPRD,
	SERVER_CFG_PARAMETER_GPSSPRD,
	SERVER_CFG_PARAMETER_SMSEN,
	SERVER_CFG_PARAMETER_SMSC,
	SERVER_CFG_PARAMETER_SMSP,
	SERVER_CFG_PARAMETER_SMSITV,
	SERVER_CFG_PARAMETER_PHOTM,
	SERVER_CFG_PARAMETER_ANSPEMT,
	SERVER_CFG_PARAMETER_PHNAUTO,
	SERVER_CFG_PARAMETER_BLKBOX,
	SERVER_CFG_PARAMETER_SORINTR,
	SERVER_CFG_PARAMETER_SPISER0,
	SERVER_CFG_PARAMETER_SPI0BAUD,
	SERVER_CFG_PARAMETER_CHKDOOR,
	SERVER_CFG_PARAMETER_PHODOOR,
	SERVER_CFG_PARAMETER_RSHDTM,
	SERVER_CFG_PARAMETER_RSMSTM,
	SERVER_CFG_PARAMETER_RSPDTM,
	SERVER_CFG_PARAMETER_RDORTM,
	SERVER_CFG_PARAMETER_RBSHDCT,
	SERVER_CFG_PARAMETER_RBMSGCT,
	SERVER_CFG_PARAMETER_ROSPDCT,
	SERVER_CFG_PARAMETER_RCDORCT,
	SERVER_CFG_PARAMETER_SDCDCHK,
	SERVER_CFG_PARAMETER_USEGPS,
	SERVER_CFG_PARAMETER_DFTSPD,
	SERVER_CFG_PARAMETER_OVSPDTM,
	SERVER_CFG_PARAMETER_STAYTM,
	SERVER_CFG_PARAMETER_LEDITFC,
	SERVER_CFG_PARAMETER_LINEEXT,
	SERVER_CFG_PARAMETER_SGPSCNT,
	SERVER_CFG_PARAMETER_SIOSHD,
	SERVER_CFG_PARAMETER_LEDZJPT,
	SERVER_CFG_PARAMETER_WICLEEN,
	SERVER_CFG_PARAMETER_WICLOC,
	SERVER_CFG_PARAMETER_WICGW,
	SERVER_CFG_PARAMETER_WICAPES1,
	SERVER_CFG_PARAMETER_WICAPPS1,
	SERVER_CFG_PARAMETER_WICAPES2,
	SERVER_CFG_PARAMETER_WICAPPS2
};

#define 	DEVICE_REQUEST_REGISTER_TCP					DEVICE_ID_MASK(0X01)	/*register device requset command from device of TCP protocol*/
#define 	DEVICE_REQUEST_BEATHEART_TCP				DEVICE_ID_MASK(0X02)	/*beat heart requset command from device of TCP protocol*/
#define 	DEVICE_REALTIME_POSITION					DEVICE_ID_MASK(0X03)	/*the realtime position of device*/
#define 	DEVICE_ARRIVE_POINT_POSITION				DEVICE_ID_MASK(0X04)	/*the position information of device arrives fixed point*/
#define 	DEVICE_LEAVE_POINT_POSITION					DEVICE_ID_MASK(0X05)	/*the position information of device leaves fixed point*/
#define 	DEVICE_ABNORMAL_DATA						DEVICE_ID_MASK(0X06)	/*the abnormal data of device, for example alarm occurs*/
#define 	DEVICE_RESPONSE_SUCCESS						DEVICE_ID_MASK(0X07)	/*the success response of device action*/
#define 	DEVICE_RESPONSE_FAILURE						DEVICE_ID_MASK(0X08)	/*the failure response of device action*/
#define 	DRIVER_CHECKING_IN							DEVICE_ID_MASK(0X09)	/*the check on work attendance of driver*/
#define 	DEVICE_SETTING_RESPONSE						DEVICE_ID_MASK(0X0A)	/*the response of setting/getting parameter of device*/
#define 	DEVICE_VERSIION_RESPONSE					DEVICE_ID_MASK(0X0B)	/*the response of getting version of device*/
#define 	DEVICE_IMAG_TRANSFER_RESPONSE				DEVICE_ID_MASK(0X0C)	/*the response of transfering imag of device*/
#define 	DEVICE_NORMAL_DATA							DEVICE_ID_MASK(0X0D)	/*the normal data of device*/
#define 	DEVICE_GPS_ORIGINAL_DATA					DEVICE_ID_MASK(0X0E)	/*the original data of GPS of device*/
#define 	DEVICE_LINE_INFORMATION						DEVICE_ID_MASK(0X0F)	/*the line information of device when server requests*/
#define 	DEVICE_VEHICLE_LOG_FILE						DEVICE_ID_MASK(0X10)	/*the log file of vehicle in device*/
#define 	DEVICE_ICCARD_DATA							DEVICE_ID_MASK(0X11)	/*the IC card data of device, when driver checks in manually*/
#define 	DEVICE_JOIN_BUSINESS						DEVICE_ID_MASK(0X12)	/*the device apply in to add business*/
#define 	DEVICE_EXIT_BUSINESS						DEVICE_ID_MASK(0X13)	/*the device apply in to exit business*/
#define 	DEVICE_CHANGE_LINE_PACKAGE					DEVICE_ID_MASK(0X14)	/*the device send line package when change line*/
#define 	DEVICE_SEND_LINE_NAME						DEVICE_ID_MASK(0X15)	/*the device send line name when change line*/
#define 	DEVICE_RECORDER_STATUS_RESPONSE				DEVICE_ID_MASK(0X16)	/*the device returns recorder status*/
#define 	DEVICE_REALTIME_POSITION_CAN				DEVICE_ID_MASK(0X17)	/*the realtime position of device with can*/
#define 	DEVICE_SERIAL_PORT_TRANSMITION				DEVICE_ID_MASK(0X18)	/*the device transmits data by serial port*/
#define 	DEVICE_SEND_COIN_MACHINE_RESPONSE			DEVICE_ID_MASK(0X19)	/*the device transmits the response data by coin machine*/

#define B2W(b) (*(b) | (*((b)+1) << 8))
#define B2DW(b) (*(b) | (*((b)+1) << 8) | (*((b)+2) << 16) | (*((b)+3) << 24))

#define W2B(b, w) {(b)[0] = (w) & 0xFF; (b)[1] = ((w)>>8) & 0xFF}
#define DW2B(b, dw) {(b)[0] = (dw) & 0xFF; (b)[1] = ((dw)>>8) & 0xFF; (b)[2] = ((dw)>>16) & 0xFF; (b)[3] = ((dw)>>24) & 0xFF}

#define DBG_DOWN_MSG
#define BUS_NET_TRACE()		printf("[TRACE]: File is %s @line %d\r\n", __FILE__, __LINE__)

struct downlink {
	unsigned int id;
	unsigned int data_size;
	void *data;
	unsigned int extra_size;
	void *extra;
};

struct extra {
	unsigned short usrid;
	unsigned short cmdsn;
};

struct camera {
	unsigned char id;
	unsigned char action;
};

struct line {
	unsigned int id;
	unsigned char flag;
};

struct coin_machine_query {
	unsigned char reply_mark;
	unsigned int reply_sn;
	unsigned char reserve[7];
};

struct coin_machine_setting {
	struct date {
		unsigned char year;
		unsigned char mon;
		unsigned char day;
	};

	struct time {
		unsigned char hour;
		unsigned char min;
		unsigned char sec;
	};
	struct date modif_date;
	struct time modif_time;
	struct time start_time;
	struct time end_time;

	unsigned char remote_ctrl;
	unsigned int lat;
	unsigned int lon;
	unsigned int range;
	unsigned char reserve[41];
};

struct upgrade {
	char upgrade_ip[20];
	unsigned short upgrade_port;
	char log_ip[20];
	unsigned short log_port;
	char upgrade_username[20];
	char upgrade_password[20];
};

struct led_content {
#define DISPLAY_MODE_TOP2BOTTOM	0x00
#define DISPLAY_MODE_BOTTOM2TOP	0x01
#define DISPLAY_MODE_LEFT2RIGHT	0x02
#define DISPLAY_MODE_RIGHT2LEFT	0x03
#define DISPLAY_MODE_FLASH		0x04

	unsigned char id;
	unsigned char priority;
	unsigned char display_mode;
	unsigned char font_size;
	unsigned char mute_mode; //0 mute 1 no mute
	unsigned short auto_clear_sec; //0 no clear
	unsigned short content_len;
	unsigned char content[256];
};

struct show {
	unsigned char data_len;
	unsigned data[256];
};

struct utt {
	unsigned char data_len;
	unsigned data[256];
};

struct pos_trace {
	unsigned int count;
	unsigned int time_interval;
};

class Bus_net {
public:
	Bus_net();
	Bus_net(const char *, unsigned short, Queue *);
	~Bus_net();
	void net_start();
	void net_join();
	pthread_t get_up_tid();
	pthread_t get_down_tid();
	int net_connect();
	Queue* get_up_queue();
	Queue* get_down_queue();
	Queue* set_down_queue();

public:
	void login();
	void parse(unsigned char *data, unsigned int len);

private:
	int get_type(unsigned char, int *, unsigned char *);
	int swtype(unsigned char, int, unsigned char *, unsigned int);

private:
	int atscanf(char*, const char *, ...);
	int at_parse(char *, msg_t);

private:
	Queue *up_queue;
	Queue *down_queue;

public:
#define DOWN_BUFF_SIZE (1024 * 2)
	int sockfd;
	char ip[20];
	unsigned short port;
	unsigned char *down_bptr;
	unsigned int down_blen;

private:
	pthread_t up_tid;
	pthread_t down_tid;

};

#endif /* BUS_NET_H_ */
